'''
Created on Aug 25, 2011

@author: david
'''

from pspy.gae.actiondomain import PspyAction

class Scan(PspyAction):
    """
    Default Action 
    spawns index.gen
    """
    def execute(self): 
        """
        Receive results of scan...
        """
        import logging
        from app.Ranger.agents import AppAgent, AccessAgent
        from pspy import Notification
        
        req = self.request
        did = req.getParameter("did")
        aid = req.getParameter("aid")
        pin = req.getParameter("pin")
        vin = req.getParameter("vin","NO_VIN")
        
        logging.info("~~ VIN: %s (%s %s %s)" % (vin,pin,aid,did))
        
        # check for missing session
        try:
            if not req.session.has_key("acct"):
                # attempt to rebuild
                acct = AccessAgent().fetchAccount(aid)
                req.session['acct'] = acct.aid
                req.session['uid'] = acct.uid
            
            req.setAttribute("dealers", AppAgent().getDealers())
            req.setAttribute("acct",aid)
            req.setAttribute("results","100::Ok")
            req.setAttribute("full",1)
            req.setResponsePage("/Ranger/Home.gen")        
            
        except Exception, ex:
            Notification("Error Fetching Account: %s" % aid).setException(ex)
            req.setAttribute("results","500::%s" % str(ex))
            req.setResponsePage("/site_error.gen")        
        
        # all done
        return req.getResponse()
    
    def sess_check(self):
        return True